import base64
import pickle
from django_redis import get_redis_connection


def merge_cart_cookie_to_redis(request, user, response):
    cookie = request.COOKIES.get('carts')
    if not cookie:
        return response

    cookie = pickle.loads(base64.b64decode(cookie.encode()))

    new_cart_dict = {}
    new_cart_selected_add = []
    new_cart_selected_rem = []

    for sku_id, cookie_dict in cookie.items():
        new_cart_dict[sku_id] = cookie_dict['count']

        if cookie_dict['selected']:
            new_cart_selected_add.append(sku_id)
        else:
            new_cart_selected_rem.append(sku_id)

    redis_conn = get_redis_connection('carts')
    pl = redis_conn.pipeline()
    pl.hmset('carts_%s' % user.id, new_cart_dict)

    if new_cart_selected_add:
        pl.sadd('selected_%s' % user.id, *new_cart_selected_add)
    else:
        pl.srem('selected_%s' % user.id, *new_cart_selected_rem)

    pl.execute()

    response.delete_cookie('carts')
    return response
